package com.soar.tbox.bean;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.soar.tbox.util.Converts;

/**
 * 指令对象
 * @author songjunjie
 *
 */
public class Command {
	/**
	 * 指令头
	 */
	private byte head;
	/**
	 * 传输方向
	 */
	private byte transmitFlag;
	/**
	 * 业务类别
	 */
	private byte type;
	/**
	 * 指令编号
	 */
	private byte commandNo;
	/**
	 * 年
	 */
	private byte year;
	/**
	 * 月日时分秒
	 */
	private byte[] time;
	/**
	 * 状态标识(应答指令才有)
	 */
	private byte statusFlag;
	/**
	 * 标识(应答指令才有) B7位： 0b，表示短参数模式；1b，表示长参数模式。
	 */
	private byte markBit;
//	private List<byte[]> param = new ArrayList<byte[]>();
	
	private List<Paramter> paramList = new ArrayList<Paramter>();

	public byte getHead() {
		return head;
	}

	public void setHead(byte head) {
		this.head = head;
	}

	public byte getTransmitFlag() {
		return transmitFlag;
	}

	public void setTransmitFlag(byte transmitFlag) {
		this.transmitFlag = transmitFlag;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public byte getCommandNo() {
		return commandNo;
	}

	public void setCommandNo(byte commandNo) {
		this.commandNo = commandNo;
	}

	public byte getYear() {
		return year;
	}

	public void setYear(byte year) {
		this.year = year;
	}

	public byte[] getTime() {
		return time;
	}

	public void setTime(byte[] time) {
		this.time = time;
	}

	public byte getStatusFlag() {
		return statusFlag;
	}

	public void setStatusFlag(byte statusFlag) {
		this.statusFlag = statusFlag;
	}

	public byte getMarkBit() {
		return markBit;
	}

	public void setMarkBit(byte markBit) {
		this.markBit = markBit;
	}

//	public List<byte[]> getParam() {
//		return param;
//	}

//	public void setParam(List<byte[]> param) {
//		this.param = param;
//	}
//	
//	public void addParam(byte[] param){
//		this.param.add(param);
//	}
	
	public List<Paramter> getParamterList(){
		return this.paramList;
	}
	
	public void setParamList(List<Paramter> paramList){
		this.paramList = paramList;
	}
	
	public void addParam(Paramter param){
		this.paramList.add(param);
	}
	
	/**
	 * 将指令转换成字节数组
	 * @return
	 * @throws IOException
	 */
	public byte[] toByteArray() throws IOException{
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		out.write(this.head);
		out.write(this.transmitFlag);
		out.write(this.type);
		out.write(this.commandNo);
		out.write(this.year);
		out.write(this.time);
		out.write(this.statusFlag);
		out.write(this.markBit);
//		for(byte[] p : param){
//			out.write(p);
//		}
		
		for(Paramter param : paramList){
			byte[] paramArray = param.toByteArray();
			out.write(paramArray);
		}
		return out.toByteArray();
	}
	
	/**
	 * 将指令转换成十六进制的字符串的形式
	 * @return
	 * @throws IOException
	 */
	public String toHexString() throws IOException{
		byte[] cmd = this.toByteArray();
		String cmdStr = Converts.printHexBinary(cmd);
		return cmdStr;
	}
	
	/**
	 * 给参数总流水号的实际值。
	 * @param serialNum
	 */
	public void setSerialNum(String serialNum){
		List<Paramter> paramlist = getParamterList();
		// 给请求指令设置流水号
		for (Paramter param : paramlist) {
			byte[] key = param.getKey();
			if (key[0] == 0x06) {// 表示是流水号
				param.setValue(serialNum.getBytes());
			}
			break;
		}
	}
}
